# -*- coding:utf-8 -*-
import FileTree
import my_log
import os

class FiletreeCompare(object):
    old_file_paths=[]
    new_file_paths=[]
    old_file_tree = FileTree.FileTree()
    new_file_tree = FileTree.FileTree()

    diff_file_tree = FileTree.FileTree()

    def iter_node(self,old_node = FileTree.Node(),new_node = FileTree.Node()):
        if old_node == None:
            assert new_node != None
            self.diff_file_tree.apply_path(new_node.get_full_path(), "+")
            return
        if new_node == None:
            assert old_node != None
            self.diff_file_tree.apply_path(old_node.get_full_path(), "-")
            return

        assert new_node != None and old_node != None
        #assert new_node.get_name() == old_node.get_name()

        if new_node.is_dir() == True and old_node.is_dir() == True:
            for key in new_node.child_node.keys(): #all new key (overlap new and old)
                if key == "..":
                    continue
                self.iter_node(old_node.child_node.get(key),new_node.child_node.get(key))
            for key in old_node.child_node.keys(): # other old key (exclude overlap)
                if key not in new_node.child_node.keys():
                    _remove_in_new_path = new_node.get_full_path() + key
                    if old_node.child_node[key].is_dir():
                        _remove_in_new_path += "/"
                    self.diff_file_tree.apply_path(_remove_in_new_path, "-")
            return
        if new_node.is_dir() == False and old_node.is_dir() == False:
            old_data = old_node.get_data()
            new_data = new_node.get_data()
            assert old_data != None and new_data != None
            old_datas = old_data.split("-")
            new_datas = new_data.split("-")
            assert len(old_datas) == 3 and len(new_datas) == 3
            old_size_md5 = old_datas[0] + old_datas[2]
            new_size_md5 = new_datas[0] + new_datas[2]
            if old_size_md5 != new_size_md5:
                self.diff_file_tree.apply_path(new_node.get_full_path(), "*")
            return
        assert (new_node.is_dir() == True and old_node.is_dir() == False) or (new_node.is_dir() == False and old_node.is_dir() == True)
        if new_node.is_dir() == True and old_node.is_dir() == False:
            self.diff_file_tree.apply_path(new_node.get_full_path(), ">")
        else:
            self.diff_file_tree.apply_path(new_node.get_full_path(), "<")
        return

    def check_path(self, path_old="", path_new=""):
        old_node = self.old_file_tree.get_node(path_old)
        new_node = self.new_file_tree.get_node(path_new)
        #assert old_node!=None and new_node != None
        self.iter_node(old_node,new_node)


    def compare(self):
        self.diff_file_tree = FileTree.FileTree()
        if len(self.old_file_paths) == 0:
            my_log.debug("paths should be added first\n")
            return
        if len(self.old_file_paths) != len(self.new_file_paths):
            my_log.debug("it cann not be compared even their have no same number of paths\n")
            return

        I = len(self.old_file_paths)
        for i in range(0,I):
            self.check_path(self.old_file_paths[i],self.new_file_paths[i])
        pass